package com.wanmait.myhouse.managerController;

import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.dao.UserDAO;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import com.wanmait.myhouse.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("manager")
public class UserController {

    @GetMapping("userPage")
    public String userPage(Integer pno , Model model){
        UserDAO userDAO = new UserDAO();
        Pager pager = new Pager();
        if (pno == null){
            pno = 1;
        }
        pager.setPageNO(pno);
        pager.setPageSize(4);
        int total = userDAO.getTotal();
        int pageCount = total/pager.getPageSize()+(total%pager.getPageSize()==0?0:1);
        pager.setPageCount(pageCount);
        model.addAttribute("pager",pager);
        List<User> userList = userDAO.findByPagen(pager);
        model.addAttribute("userList",userList);
        return "/manager/user";
    }

    @GetMapping("banUser")
    public String banUser(Integer id, Boolean ban, HttpServletRequest request){
        String uri = request.getHeader("referer");
        if (ban == true){
            ban = false;
        }else {
            ban = true;
        }
       UserDAO userDAO = new UserDAO();
        userDAO.banUser(id,ban);
        return "redirect:"+uri;
    }

}
